package com.gomro.galaxy.foundation.bi.engine.analysis;

import cn.gomro.cloud.core.common.utils.DateUtils;
import cn.gomro.cloud.core.common.utils.EncryptUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gomro.galaxy.core.batch.ElasticSearchClzItemReader;
import com.gomro.galaxy.core.batch.ElasticSearchClzItemWriter;
import com.gomro.galaxy.core.commons.elastic.ElasticSearchRest;
import com.gomro.galaxy.foundation.bi.domain.Constants;
import com.gomro.galaxy.foundation.bi.domain.enumerater.GoodsStatisticsType;
import com.gomro.galaxy.foundation.bi.domain.event.GoodsTradingVolumeEvent;
import com.gomro.galaxy.foundation.bi.domain.model.AcquireOrderGoods;
import com.gomro.galaxy.foundation.bi.domain.model.AcquirePurchaseGoods;
import com.gomro.galaxy.foundation.bi.domain.model.analysis.MarketGoodsTradingVolume;
import com.gomro.galaxy.foundation.bi.engine.AnalysisLastTime;
import com.gomro.galaxy.foundation.bi.engine.model.AnalysisCache;
import com.gomro.galaxy.foundation.bi.engine.query.GoodsQuery;
import com.gomro.galaxy.foundation.bi.engine.query.PurchaseGoodsQuery;
import com.gomro.galaxy.foundation.bi.engine.util.BoolQueryBuilders;
import com.gomro.galaxy.foundation.bi.engine.util.EngineUtils;
import com.gomro.galaxy.foundation.bi.exception.BiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.Sum;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;

import java.time.ZoneId;
import java.util.*;

import static com.gomro.galaxy.foundation.bi.engine.util.EngineUtils.MAX_BATCH_NUM;

/**
 * 商品交易量统计
 * @Author: zhoujian
 * @Date: 2020/4/10 13:54
 * @Version 1.0
 */
@Slf4j
@Configuration
public class GoodsTradingVolumeJob {
    private static final String BiAnalysisLastName = Constants.BiDataMartIndexPrefix + AnalysisLastTime.TableName;
    private static final String BiWarehousePurchaseGoodsName = Constants.BiWarehouseIndexPrefix + AcquirePurchaseGoods.TableName;
    private static final String BiWarehouseOrderGoodsName = Constants.BiWarehouseIndexPrefix + AcquireOrderGoods.TableName;
    private static final String BiDataMartName = Constants.BiDataMartIndexPrefix + MarketGoodsTradingVolume.TableName;
    private static final String JobName = Constants.JobBeanNamePrefix + MarketGoodsTradingVolume.TableName;
    private static final String StepName1 = Constants.StepBeanNamePrefix + MarketGoodsTradingVolume.TableName + 1;
    private static final String StepName2 = Constants.StepBeanNamePrefix + MarketGoodsTradingVolume.TableName + 2;
    private static final String ReaderName1 = Constants.ReaderBeanNamePrefix + MarketGoodsTradingVolume.TableName + 1;
    private static final String ReaderName2 = Constants.ReaderBeanNamePrefix + MarketGoodsTradingVolume.TableName + 2;
    private static final String WriterName1 = Constants.WriterBeanNamePrefix + MarketGoodsTradingVolume.TableName + 1;
    private static final String WriterName2 = Constants.WriterBeanNamePrefix + MarketGoodsTradingVolume.TableName + 2;
    private static final String ProcessName1 = Constants.ProcessBeanNamePrefix + MarketGoodsTradingVolume.TableName + 1;
    private static final String ProcessName2 = Constants.ProcessBeanNamePrefix + MarketGoodsTradingVolume.TableName + 2;

    private ApplicationEventPublisher applicationEventPublisher;
    private BusProperties busProperties;
    private JobBuilderFactory jobBuilderFactory;
    private StepBuilderFactory stepBuilderFactory;
    private ElasticSearchRest rest;
    private RestHighLevelClient client;
    private ObjectMapper mapper;
    private AnalysisCache<MarketGoodsTradingVolume> cache = new AnalysisCache<>();

    public GoodsTradingVolumeJob(ApplicationEventPublisher applicationEventPublisher, BusProperties busProperties, JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, RestHighLevelClient client, ObjectMapper mapper) {
        this.applicationEventPublisher = applicationEventPublisher;
        this.busProperties = busProperties;
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;

        this.client = client;
        this.mapper = mapper;
        this.rest = new ElasticSearchRest(client, mapper);
    }

    @Bean(JobName)
    public Job job(@Qualifier(StepName1) Step step1, @Qualifier(StepName2) Step step2) {

        return jobBuilderFactory.get(JobName)
                .incrementer(new RunIdIncrementer())
                .start(step1).on("COMPLETED").to(step2)
                .end()
                .listener(jobExecutionListener)
                .build();
    }

    @Bean(StepName1)
    public Step step1() {

        return stepBuilderFactory.get(StepName1)
                .<AcquirePurchaseGoods, MarketGoodsTradingVolume>chunk(MAX_BATCH_NUM)
                .reader(reader1())
                .processor(processor1())
                .writer(writer1())
                .build();
    }

    @Bean(StepName2)
    public Step step2() {

        return stepBuilderFactory.get(StepName2)
                .<MarketGoodsTradingVolume, MarketGoodsTradingVolume>chunk(MAX_BATCH_NUM)
                .reader(reader2())
                .processor(processor2())
                .writer(writer2())
                .build();
    }

    @Bean(ReaderName1)
    @StepScope
    public ElasticSearchClzItemReader<AcquirePurchaseGoods> reader1() {

        // 获取最后更新时间
        AnalysisLastTime analysisLastTime = rest.documentGet(BiAnalysisLastName, MarketGoodsTradingVolume.TableName, AnalysisLastTime.class);
        Date last;
        if (analysisLastTime != null && analysisLastTime.getLast() != null) {
            last = analysisLastTime.getLast();
        } else {
            ElasticSearchRest.SearchResultClz<AcquirePurchaseGoods> data = rest.searchAsClz(BiWarehousePurchaseGoodsName, ElasticSearchRest.Page.of(1, 1), null, Collections.singleton(SortBuilders.fieldSort("time").order(SortOrder.ASC)), null, AcquirePurchaseGoods.class);
            if (Objects.isNull(data) || CollectionUtils.isEmpty(data.getData())) {
                throw new BiException("analysisLastTime init error");
            }
            last = Date.from(data.getData().get(0).getTime().atZone(ZoneId.systemDefault()).toInstant());
        }
        //初始化缓存, 不能并行任务
        if (Objects.isNull(cache.getCache())) {
            cache.setCache(new HashMap<>());
        }
        cache.setLast(last);
        PurchaseGoodsQuery.PurchaseGoodsQueryBuilder builder = PurchaseGoodsQuery.builder().del(false).vmiGoods(false);
        //查询数量小于MAX_BATCH_NUM时返回null
        Date endDate = EngineUtils.getEndDate(last, (date, date2) -> rest.searchCount(BiWarehousePurchaseGoodsName,
                BoolQueryBuilders.purchaseGoods(builder.start(date).end(date2).build())));
        if (Objects.nonNull(endDate)) {
            cache.setLast(endDate);
        } else {
            endDate = new Date();
            cache.setLast(endDate);
        }
        ElasticSearchClzItemReader<AcquirePurchaseGoods> reader = new ElasticSearchClzItemReader<>(client, mapper, BiWarehousePurchaseGoodsName,
                MAX_BATCH_NUM, BoolQueryBuilders.purchaseGoods(builder.start(last).end(endDate).build()), Collections.singleton(SortBuilders.fieldSort("id").order(SortOrder.ASC)), AcquirePurchaseGoods.class);
        reader.setName(ReaderName1);
        return reader;
    }

    @Bean(ProcessName1)
    public ItemProcessor<AcquirePurchaseGoods, MarketGoodsTradingVolume> processor1() {
        return acquirePurchaseGoods -> {
            String id = EncryptUtils.MD5_32(GoodsStatisticsType.Purchase.name() + acquirePurchaseGoods.getId());
            return MarketGoodsTradingVolume.builder()
                    .id(id)
                    .brand(acquirePurchaseGoods.getBrand())
                    .name(acquirePurchaseGoods.getName())
                    .model(acquirePurchaseGoods.getModel())
                    .vendorId(acquirePurchaseGoods.getVendorId())
                    .vendorName(acquirePurchaseGoods.getVendorName())
                    .goodsStatisticsType(GoodsStatisticsType.Purchase)
                    .purchaseGoodsId(acquirePurchaseGoods.getId())
                    .time(acquirePurchaseGoods.getTime().toLocalDate())
                    .quotationNum(acquirePurchaseGoods.getNum())
                    .quotationTotal(acquirePurchaseGoods.getTotal())
                    .orderGoodsNum(0.0)
                    .orderGoodsTotal(0.0)
                    .time(acquirePurchaseGoods.getTime().toLocalDate())
                    .build();
        };
    }

    @Bean(WriterName1)
    public ItemWriter<MarketGoodsTradingVolume> writer1() {
        return list -> list.forEach(e -> cache.getCache().put(e.getId(), e));
    }

    @Bean(ReaderName2)
    @StepScope
    public ItemReader<MarketGoodsTradingVolume> reader2() {
        return EngineUtils.createCacheReader(cache.getCache());
    }

    @Bean(ProcessName2)
    public ItemProcessor<MarketGoodsTradingVolume, MarketGoodsTradingVolume> processor2() {
        return marketGoodsTradingVolume -> {
            Long goodsId = marketGoodsTradingVolume.getPurchaseGoodsId();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.filter(QueryBuilders.termQuery("goodsId", goodsId));
            boolQueryBuilder.filter(QueryBuilders.termQuery("del", false));

            AggregationBuilder sumNum = AggregationBuilders.sum("num").field("num");
            AggregationBuilder sunTotal = AggregationBuilders.sum("total").field("total");

            SearchResponse search = rest.search(BiWarehouseOrderGoodsName, ElasticSearchRest.Page.of(1, 0),
                    boolQueryBuilder, null, Arrays.asList(sumNum, sunTotal), null);
            Map<String, Aggregation> stringAggregationMap = search.getAggregations().asMap();
            Aggregation num = stringAggregationMap.getOrDefault("num", null);
            Aggregation total = stringAggregationMap.getOrDefault("total", null);
            if (Objects.nonNull(num)) {
                marketGoodsTradingVolume.setOrderGoodsNum(((Sum) num).getValue());
            }
            if (Objects.nonNull(total)) {
                marketGoodsTradingVolume.setOrderGoodsTotal(((Sum) total).getValue());
            }
            return marketGoodsTradingVolume;
        };
    }

    @Bean(WriterName2)
    public ElasticSearchClzItemWriter<MarketGoodsTradingVolume> writer2() {

        return new ElasticSearchClzItemWriter<>(client, mapper, BiDataMartName);
    }

    private JobExecutionListener jobExecutionListener = new JobExecutionListener() {

        @Override
        public void beforeJob(@NonNull JobExecution jobExecution) {

            ElasticSearchRest rest = new ElasticSearchRest(client, mapper);

            // 初始化数据索引
            if (!rest.indexExists(BiDataMartName)) {

                rest.indexCreate(BiDataMartName);
                rest.indexMappingPut(BiDataMartName, "analysis/" + BiDataMartName + ".json", true);
                rest.indexFlush(BiDataMartName, null, null);
            }

            // 初始化最后更新时间索引
            if (!rest.indexExists(BiAnalysisLastName)) {

                rest.indexCreate(BiAnalysisLastName);
                rest.indexMappingPut(BiAnalysisLastName, "analysis/" + BiAnalysisLastName + ".json", true);
                rest.indexFlush(BiAnalysisLastName, null, null);
            }
        }

        @Override
        public void afterJob(@NonNull JobExecution jobExecution) {

            // 回写最后更新时间
            if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
                log.debug("last time ---> {}", DateUtils.dateToStr(cache.getLast()));
                rest.documentCreate(BiAnalysisLastName, MarketGoodsTradingVolume.TableName, AnalysisLastTime.builder().id(MarketGoodsTradingVolume.TableName).last(cache.getLast()).build());
                applicationEventPublisher.publishEvent(new GoodsTradingVolumeEvent(cache.getLast()));
            }
            if (Objects.nonNull(cache.getCache())) {
                log.debug("cache size -> {}", cache.getCache().size());
                cache.getCache().clear();
            }
        }
    };
}
